﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Kotonoha.Objects.Model.Event {
    public abstract class Event {

        /// <summary>
        /// for the sake of the ORM, don't use otherwise
        /// </summary>
        public long Id { get; set; }

        public DateTime Time { get; set;}

        public virtual void Serialize(TextWriter writer) {
            writer.Write("{0:o}", Time);
            writer.Write(";");
            serializeInner(writer);
            writer.WriteLine();
        }

        protected abstract void serializeInner(TextWriter writer);

        public virtual void Deserialize(TextReader reader) {
            string s =  reader.ReadLine();
            int pos = s.IndexOf(';');
            Time = DateTime.Parse(s.Substring(0, pos));
            deserializeInner(new StringReader(s.Substring(pos + 1)));
        }

        protected abstract void deserializeInner(TextReader reader);

        public virtual void apply(Word word) {
            //does nothing
        }

        public virtual void apply(Container container) {
        }

        public bool Equals(Event obj) {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            return obj.Time.Equals(Time);
        }

        public override bool Equals(object obj) {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            if (obj.GetType() != typeof (Event)) {
                return false;
            }
            return Equals((Event) obj);
        }

        public override int GetHashCode() {
            return Time.GetHashCode();
        }

    }
}
